<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class OutgoingAcceptTransformer extends OutgoingTransformer {
	
	public $xmlData;
	
	public function __construct($accept_response_id_array, $remote_url, $accept_response_index_authentication = 0, $table_direction = 'Outgoing') {
		
		$member_direction = 'outgoing';
		
		if ($table_direction == 'Incoming'):
			$accept_response_factory 		= IncomingAcceptResponseFactory::get_instance();
			$accept_response_host_factory 	= IncomingAcceptResponseHostFactory::get_instance();
			$member_direction = 'incoming';
		else:
			$accept_response_factory 		= OutgoingAcceptResponseFactory::get_instance();
			$accept_response_host_factory 	= OutgoingAcceptResponseHostFactory::get_instance();
		endif;
		
		if (!is_array($accept_response_id_array)):
			
			$accept_response_id_array = array($accept_response_id_array);
			
		endif;
		
   		$dom_document = new DomDocument("1.0", "ISO-8859-1");

		$dom_element_dadf_accept 	= $dom_document->createElement('dadf-accept');

	    $params[$table_direction . "AcceptResponseID"] = $accept_response_id_array[$accept_response_index_authentication]; 
	    $accept_response_auth = $accept_response_factory->get_row($params);
	    $accept_response_auth->load_children();

	    $member_name 		 		= 'class_member_only_' . $member_direction . '_accept_authentication';
	    
		if ($accept_response_auth->$member_name != null):
		
			$dom_element_authentication 	= $dom_document->createElement('authentication');
			$dom_element_username 			= $dom_document->createElement('username', $accept_response_auth->$member_name->Username);
			$dom_element_authentication->appendChild($dom_element_username);
			// change to plain text pass for outgoing

			if ($table_direction == 'Incoming'):
				$dom_element_password 		= $dom_document->createElement('password', $accept_response_auth->$member_name->PasswordMD5);
			else:
				$dom_element_password 		= $dom_document->createElement('password', EncryptUtils::decrypt_pass($accept_response_auth->$member_name->PasswordEncoded));
			endif;
						
			$dom_element_authentication->appendChild($dom_element_password);
			$dom_element_dadf_accept->appendChild($dom_element_authentication);
			
		endif;

		$dom_element_campaignactions 				= $dom_document->createElement('campaignactions');
		
		//echo(count($bid_request->class_member_only_outgoing_request_adbundle_list));
		
		foreach ($accept_response_id_array as $accept_response_id):
		
	    	$params[$table_direction . "AcceptResponseID"] = $accept_response_id; 
	    	$accept_response = $accept_response_factory->get_row($params);
	    	$accept_response->load_children();
		
	    	
			$params = array();
			$column_name 								= $table_direction . 'AcceptResponseID';
			$params[$column_name] 						= $accept_response->$column_name;
			$accept_response_host 						= $accept_response_host_factory->get_row($params);
		    	
			if ($accept_response_host == null):
				
				$class_name 								= $table_direction .  'AcceptResponseHost';
				$accept_response_host 						= new $class_name();
				$column_name 								= $table_direction . 'AcceptResponseID';
				$accept_response_host->$column_name			= $accept_response->$column_name;
				
			endif;
			
			if ($table_direction == 'Incoming'):
				$accept_response_host->RemoteHostAddr		= $remote_url; 
			else:
				$accept_response_host->RemoteURL			= $remote_url; 
			endif;
			
			$accept_response_host->HttpResponseCode		= 400;
			$accept_response_host->save();
	    	
			$dom_element_campaignaction 	= $dom_document->createElement('campaignaction');
			
			$dom_element_acceptid			= $dom_document->createElement('acceptid', $accept_response->AcceptID);
			$dom_element_campaignaction->appendChild($dom_element_acceptid);
			$dom_element_id					= $dom_document->createElement('id', $accept_response->ID);
			$dom_element_campaignaction->appendChild($dom_element_id);
			$dom_element_action				= $dom_document->createElement('action', $accept_response->Action);
			$dom_element_campaignaction->appendChild($dom_element_action);
			$dom_element_partnerid			= $dom_document->createElement('partnerid', $accept_response->PartnerID);
			$dom_element_campaignaction->appendChild($dom_element_partnerid);
			$dom_element_partnername		= $dom_document->createElement('partnername', $accept_response->PartnerName);
			$dom_element_campaignaction->appendChild($dom_element_partnername);

			$dom_element_campaignactions->appendChild($dom_element_campaignaction);
			
		endforeach;
		
		$dom_element_dadf_accept->appendChild($dom_element_campaignactions);
		$dom_document->appendChild($dom_element_dadf_accept);
		$dom_document->formatOutput = true;
		
		$dadf_version = Zend_Registry::get('application.version');
		
		if ($dom_document->schemaValidate ('../application/schemas/Dadf.accept-' . $dadf_version . '.xsd') == false):
			
			throw new Exception("DADF schema validation failed");
			
		endif;
		
		$this->xmlData  = $dom_document->saveXML();
	

	}		
	
}
			